import axios from "axios";
import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);

//获取熊猫优选接口
const ajax = axios.create({
  baseURL: "http://www.xiongmaoyouxuan.com/api"
});

// 请求拦截
ajax.interceptors.request.use(config => {
  //vant的一个加载插件加载效果
  Toast.loading({
    message: "加载中...",
    forbidClick: true
  });
  return config;
});

//响应拦截
ajax.interceptors.response.use(resp => {
  //清除动画效果
  Toast.clear();
  //可以在这里处理全局错误
  if (resp.status === 200) {
    if (resp.data.code === 200) {
      return resp.data.data;
    }
  }
  return Promise.reject(new Error("请求服务器资源失败"));
});
//获取首页数据
export const getHome = () => ajax.get("/tab/1");
//获取首页瀑布流数据
export const getList = num => ajax.get(`/tab/1/feeds?start=${num}&sort=0`);
//获取商品分类数据
export const getMall = () => ajax.get("/tabs?sa=");
//获取商品子分类信息
export const getMallSort = id => ajax.get(`/tab/${id}?start=0`);
//获取商品信息分类数据
export const getGoodList = (url, page) => {
  const id = url.split("?")[1].split("=")[1];
  const startPage = (page - 1) * 20;
  if (url.indexOf("category") > 0) {
    return ajax.get(`/category/${id}/items?start=${startPage}&sort=0`);
  } else if (url.indexOf("column") > 0) {
    return ajax.get(`/column/${id}/items?start=${startPage}`);
  }
};
//获取商品详情页数据
export const getDetail = id => ajax.get(`/detail?id=${id}&normal=1&sa=`);
//获取搜索框数据
export const getSearch = val =>
  ajax.get(
    `/search?word=${val}&start=0&sort=0&couponOnly=NaN&minPrice=0&maxPrice=99999`
  );
// 获取验证码
export const getCode = phone => {
  return axios.get("/yun/captcha/sent", {
    params: {
      phone: phone
    }
  });
};
// 发送验证请求 paone:手机号 ma:验证码
export const getRequest = (phone, ma) => {
  return axios.get("/yun/captcha/verify", {
    params: {
      //必选参数，手机号码
      phone: phone,
      //验证码，必选参数
      captcha: ma
    }
  });
};
